package vn.bus.service;

import java.util.ArrayList;
import java.util.Hashtable;

import com.google.gson.Gson;

import vn.bus.models.StationManager;
import vn.bus.dto.StationDTO;

public class StationService {

	public ArrayList<StationDTO> getAllStations() {
		ArrayList<StationDTO> allStations = null;
		try {
			StationManager stationManager = new StationManager();
			allStations = stationManager.getAllStations();

		} catch (Exception e) {
			System.out.println("error");
		}
		return allStations;
	}
	
	public String getAllStationsJson(){
		Gson gson = new Gson();
		return gson.toJson(getAllStations());
	}
	
	public Hashtable<String, StationDTO> loadAllStations(){
		
		Hashtable<String, StationDTO> allStations = new Hashtable<String, StationDTO>();
		ArrayList<StationDTO> allStas = getAllStations();
		
		for(int i = 0; i < allStas.size(); i++){
			StationDTO sta = new StationDTO();
			sta = allStas.get(i);
			if(allStations.get(sta.getId()) == null){
				allStations.put(sta.getId(), sta);
			}
		}
		//System.out.println(allStations.size());
		return allStations;
	}
	
	public String getJsonTextSearch(String str){
		ArrayList<StationDTO> textSearchS = new ArrayList<StationDTO>();
		try {
			StationManager stationManager = new StationManager();
			textSearchS = stationManager.textSearch(str);

		} catch (Exception e) {
			System.out.println("error");
		}
		Gson gson = new Gson();
		String rs = gson.toJson(textSearchS);
		rs = rs.replaceAll("name", "value");
		return rs;
	}
	
	public String getJsonClickSearch(double lat, double lon){
		ArrayList<StationDTO> clickSearchS = new ArrayList<StationDTO>();
		try {
			StationManager stationManager = new StationManager();
			clickSearchS = stationManager.clickSearch(lat, lon);

		} catch (Exception e) {
			System.out.println("error");
		}
		Gson gson = new Gson();
		String rs = gson.toJson(clickSearchS);
		rs = rs.replaceAll("name", "value");
		return rs;
	}
	
	public StationDTO getOneInfoStation(String id){
		StationDTO s = new StationDTO();
		Hashtable<String, StationDTO> allStations = new Hashtable<String, StationDTO>();
		allStations = loadAllStations();
		s = allStations.get(id);
		return s;
	}
}
